package com.ezjie.ielts.module_read;

import java.util.LinkedHashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.constant.Constants;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.model.SubmitGroupInfo;
import com.ezjie.ielts.module_read.adapter.ReadInstantReportAdapter;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.widget.SerializableMap;
import com.ezjie.ielts.widget.TitleTextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

@ContentView(R.layout.activity_instant_report)
public class ReadInstantReportActivity extends BaseFragmentActivity {

	@ViewInject(R.id.iv_topbar_back)
	private ImageView iv_topbar_back;
	@ViewInject(R.id.tv_topbar_title)
	private TitleTextView tv_topbar_title;
	@ViewInject(R.id.lv_read_instant_report)
	private ListView lv_read_instant_report;

	private ReadInstantReportAdapter mReadInstantReportAdapter;

	private Map<Integer, SubmitGroupInfo> mSubmitReadAnswer = new LinkedHashMap<Integer, SubmitGroupInfo>();

	private int total;
	private int right;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ViewUtils.inject(this);

		init();

	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_REPORT);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_REPORT);
	}

	private void init() {
		tv_topbar_title.setText(R.string.read_instant_report_title);
		iv_topbar_back.setOnClickListener(this);

		View headerView = LayoutInflater.from(this).inflate(R.layout.layout_instant_report_header, null);
		lv_read_instant_report.addHeaderView(headerView);

		Bundle bundle = getIntent().getBundleExtra(Constants.READ_INSTANT_REPORT_BUNDLE);

		SerializableMap serializableMap2 = (SerializableMap) bundle
				.getSerializable(Constants.READ_INSTANT_REPORT_SUBMIT_ANSWER);

		mSubmitReadAnswer = serializableMap2.getMap();

		for (Integer key : mSubmitReadAnswer.keySet()) {
			SubmitGroupInfo info = mSubmitReadAnswer.get(key);
			total += info.questions.size();

			for (int i = 0; i < info.questions.size(); i++) {
				if (Boolean.valueOf(info.questions.get(i).is_right)) {
					right += 1;
				}
			}
		}

		TextView tv_read_instant_report_detail = (TextView) headerView.findViewById(R.id.tv_read_instant_report_detail);
		TextView tv_read_instant_report_level = (TextView) headerView.findViewById(R.id.tv_read_instant_report_level);
		tv_read_instant_report_detail.setText(String.format(
				getResources().getString(R.string.read_instant_report_detail), total, right));

		double percent = (right / (double) total) * 100;
		if (percent == 100) {
			tv_read_instant_report_level.setText(R.string.read_instant_report_level_perfect);
		} else if (percent >= 60 && percent < 100) {
			tv_read_instant_report_level.setText(R.string.read_instant_report_level_good_job);
		} else if (percent < 60 && percent > 0) {
			tv_read_instant_report_level.setText(R.string.read_instant_report_level_not_bad);
		} else if (percent == 0) {
			tv_read_instant_report_level.setText(R.string.read_instant_report_level_god);
		}

		mReadInstantReportAdapter = new ReadInstantReportAdapter(this, bundle);

		lv_read_instant_report.setAdapter(mReadInstantReportAdapter);

	}

	@Override
	public void onClickEvent(View view) {
		switch (view.getId()) {
		case R.id.iv_topbar_back:
			finish();
			break;
		}
	}

}
